﻿using MongoDB.Driver;
using MongoDB.Driver.Builders;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace InventoryManagementServer
{
    public static class DataStore
    {
        private static MongoDatabase db = MongoDatabase.Create("mongodb://localhost/InventoryManagement");

        public static Permission Login(User user)
        {
            db.GetCollection<User>("User").Save(user);

            return Permission.All;
        }

        public static void AddProducts(User user, Product[] products)
        {
            db.GetCollection<Product>("Product").InsertBatch(products);
        }

        public static Product[] GetProducts(User user)
        {
            return db.GetCollection<Product>("Product").FindAll().ToArray();
        }

        public static void AddPurchaseOrders(User user, Order[] orders)
        {
            db.GetCollection<Order>("PurchaseOrder").InsertBatch(orders);
        }

        public static Order[] GetPurchaseOrders(User user, DateTime date)
        {
            return db.GetCollection<Order>("PurchaseOrder").Find(
                Query.And(
                    Query.GTE("Time", date),
                    Query.LT("Time", date.AddDays(1)))).ToArray();
        }

        public static void AddSellOrders(User user, Order[] orders)
        {
            db.GetCollection<Order>("SellOrder").InsertBatch(orders);
        }

        public static Order[] GetSellOrders(User user, DateTime date)
        {
            return db.GetCollection<Order>("SellOrder").Find(
                Query.And(
                    Query.GTE("Time", date),
                    Query.LT("Time", date.AddDays(1)))).ToArray();
        }
    }
}